package com.erp.utils;

import java.io.*;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class zipUnit {
    /**
     * 压缩文件
     * @param zipFileName 压缩后全路径，带文件名
     * @param inputFile 上传到文件流
     * @return
     */
    public static void zip(String zipFileName, InputStream inputFile){
        ZipOutputStream out=null;
        try {
            out = new ZipOutputStream(new FileOutputStream(
                    zipFileName)); // 创建ZipOutputStream类对象
            zip(out, inputFile, ""); // 调用方法
            out.close(); // 将流关闭
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out!=null){
                    out.close();
                }
            }catch (Exception e){}

        }

    }

    //压缩
    public  static void zip(ZipOutputStream out, InputStream file, String base)
            throws Exception { // 方法重载
            out.putNextEntry(new ZipEntry(base)); // 创建新的进入点
            // 创建FileInputStream对象
            InputStream in =  file;
            int b; // 定义int型变量
            while ((b = in.read()) != -1) { // 如果没有到达流的尾部
                out.write(b); // 将字节写入当前ZIP条目
            }
            in.close(); // 关闭流
    }
    /**
     * 读取压缩文件
     * @param path 压缩后全路径，带文件名
     *             返回正常文件流
     * @return
     */
    public static InputStream zipOut(String path)  {
        InputStream inputStream=null;
        try {
            ZipFile zf = new ZipFile(new File(path));
            Enumeration<? extends ZipEntry> entries = zf.entries();
            ZipEntry zipEntry = entries.nextElement();
            inputStream = zf.getInputStream(zipEntry);
        }catch (Exception e){
            System.out.println(e);
        }
        return inputStream;
    }

    public static void zipOut1( InputStream inputStream,String linPath) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(new File(linPath));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        int n;
        byte[] bytes = new byte[1024];
        while ((n = inputStream.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes, 0, n);
        }
        //关闭流
        bufferedOutputStream.close();
        fileOutputStream.close();
    }
    //      MyZip book = new MyZip(); // 创建本例对象
//	        // 调用方法，参数为压缩后文件与要压缩文件
//        book.zip("hello.zip", new File("src"));
//        System.out.println("压缩完成"); // 输出信息 ,
    //解压缩
//    public void zipOut() throws IOException {
//        ZipFile zf = new ZipFile(new File("C:\\Users\\ceil\\Desktop\\ziptest"));
//        Enumeration<? extends ZipEntry> entries = zf.entries();
//        ZipEntry zipEntry = entries.nextElement();
//        InputStream inputStream = zf.getInputStream(zipEntry);
//        FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\ceil\\Desktop\\ziptest.png"));
//        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//        int n;
//        byte[] bytes = new byte[1024];
//        while ((n = inputStream.read(bytes)) != -1) {
//            bufferedOutputStream.write(bytes, 0, n);
//        }
//        //关闭流
//        bufferedOutputStream.close();
//        fileOutputStream.close();
//    }

    public URL returnClass() {
        URL resource = this.getClass().getResource("");
        return resource;
    }
}



